<?php

namespace app\admin\controller;

class Order extends Admin
{
    public function __construct()
    {
        parent::__construct();
        $this->model = 'Order';
        $this->assign("model", $this->model);
    }

    public function get_where_map()
    {
        // $page = input("param.page", 1);
        // $order = input("param.order");
        // $limit = input("param.limit", $this->default_page_items);
        // $offset = ($page - 1) * $limit;
        //
        $status = input('param.status/s');
        $startdate = input('param.startdate/s');
        $enddate = input('param.enddate/s');
        $dealerid = input('param.dealerid/d');
        $keywords = input('param.keywords/s');
        // 支付状态
        $whereMap = ['ordersn' => ['like', 'activate%'],];
        if (!is_null($status) && $status != '3') {
            $whereMap['status'] = $status;
        } else {
            if ($status == '3') {
                $whereMap['status'] = ['in', ['0', '1']];
            } else {
                $whereMap['status'] = '1';
            }
        }
        // 支付时间
        $starttime = $startdate ? strtotime($startdate) : 0;
        $endtime = $enddate ? strtotime($enddate) : 0;
        if ($starttime && $endtime) {
            $whereMap['dateline'] = ['between', [$starttime, $endtime]];
        } elseif ($starttime && !$endtime) {
            $whereMap['dateline'] = ['egt', $starttime];
        } elseif ($endtime && !$starttime) {
            $whereMap['dateline'] = ['elt', $endtime];
        }
        // 所属经销商
        if ($dealerid) {
            $userids = model('AdminUser')->where('dealerid', $dealerid)->column('userid');
            $whereMap['userid'] = ['in', $userids];
        } else {
            if ($this->uinfo['roleid'] == 4) {
                $dealerids = model('AdminUser')->where('puserid', $this->uinfo['userid'])->where('roleid', 6)->column('userid');
                $userids = model('AdminUser')->where('dealerid', 'in', $dealerids)->column('userid');
                $whereMap['userid'] = ['in', $userids];
            } elseif ($this->uinfo['roleid'] == 6) {
                $userids = model('AdminUser')->where('dealerid', $this->uinfo['userid'])->column('userid');
                $whereMap['userid'] = ['in', $userids];
            }
        }
        // 搜索关键词
        if ($keywords) {
            $where['realname'] = ['like',  "%$keywords%"];
            $userid = model('AdminUser')->where($where)->column('userid');
            if ($userid) {
                $whereMap['ordersn|userid'] = ['in', $userid];
            } else {
                $whereMap['ordersn'] = ['like', "%$keywords%"];
            }
        }
        $whereMap['ordertype'] = 1;
        return $whereMap;
        //
        // list($list, $count) = model($this->model)->admin_get_list($offset, $limit, $whereMap, $order);
        // $return = [
        //     'code' => 0,
        //     'msg' => '',
        //     'count' => $count,
        //     'data' => $list,
        // ];
        // return json($return);
    }

    public function activate()
    {
        $where['status'] = 0;
        if ($this->uinfo['roleid'] == 1) {
            $where['roleid'] = 6;
        } elseif ($this->uinfo['roleid'] == 4) {
            $where['roleid'] = 6;
            $where['puserid'] = $this->uinfo['userid'];
        } elseif ($this->uinfo['roleid'] == 6) {
            $where['userid'] = $this->uinfo['userid'];
        }
        $deallist = model('AdminUser')->where($where)->order('userid desc')->column('userid, username');
        $this->assign('deallist', $deallist);
        return $this->fetch();
    }
}
